/***************************************************************************
 *   mgpi_ehimdl.h - Events Handling Interface Model Declarations          *
 *   Copyright (C) 2008 by cmaster.matso                                   *
 *   cmaster.matso@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef _EHIMDL_H
#define _EHIMDL_H

#ifdef __cplusplus
extern "C" {
#endif

#include "../../compmdl/src/mgpi_compmdl.h"
#include "../../ievtmdl/src/mgpi_ievtmdl.h"

#pragma pack(push, 1)

    typedef struct _usrevt {
        int index;                                                              /** index in the events handling routines array */
        void *ptr;                                                              /** event data pointer */
    } usrevt_t;                                                                 /** user defined event */

    typedef struct _filter {
        Uint8 event_type;                                                       /** SDL event type code */
        MGPI_BOOL(*onEvent)(SDL_Event *, void *);                               /** events handling routine pointer */
        void *ptr;                                                              /** auxiliary pointer */
        int n;                                                                  /** number of filtered events */
    } filter_t;                                                                 /** filtered event */

    struct _asycevt {                                                           /** asynchronouse event handling data type */
        int index;                                                              /** index in the events handling routines array */
        comp_t *c;                                                              /** component for the event handling */
        SDL_Event *evt;                                                         /** SDL event pointer */
        filter_t *f;                                                            /** filtered event pointer */
    };

#pragma pack(pop)

    Uint32 MGPI_API g_nInterval;                                                /** timer interval */

#define MGPI_TIMER_EVENTS   20000                                               /** timer events handling mode */
#define MGPI_THREAD_EVENTS  20001                                               /** seperate thread events handling mode */
#define MGPI_FILTERED_EVENTS    20002                                           /** filtered events handling mode */

#define MGPI_TIMER_INTERVAL g_nInterval                                         /** timer interval property */

    /**
     * Initializes the events handling thread.
     */
    void MGPI_API MGPI_InitEvents(int mode, void *ptr);

    /**
     * Turns on and off the asynchronous event handing.
     */
    void MGPI_API MGPI_AsyncProc(void);

    /**
     * Frees the events handling timer.
     */
    void MGPI_API MGPI_FreeTimer(void);
    
    /**
     * Listens to the SDL events over a container.
     */
    MGPI_BOOL MGPI_API MGPI_Listener(cont_t *c);

    /**
     * Listens to the SDL user defined events over a container.
     */
    MGPI_BOOL MGPI_API MGPI_UserListener(cont_t *c);

    /**
     * Listens to all events and fires actions for those which are defined in the filters array.
     */
    MGPI_BOOL MGPI_API MGPI_FilteredListener(filter_t *filter, int n);

/** below - standard system events codes */
    
#define MGPI_ACTIVE_EVENT   0
#define MGPI_MOUSE_BUTTON_DOWN  1
#define MGPI_MOUSE_BUTTON_UP    2
#define MGPI_MOUSE_MOTION   3
#define MGPI_KEY_DOWN   4
#define MGPI_KEY_UP 5
#define MGPI_JOY_AXIS_MOTION    6
#define MGPI_JOY_BALL_MOTION    7
#define MGPI_JOY_HAT_MOTION 8
#define MGPI_JOY_BUTTON_DOWN    9
#define MGPI_JOY_BUTTON_UP  10
#define MGPI_VIDEO_RESIZE   11
#define MGPI_VIDEO_EXPOSE   12
#define MGPI_SYS_WM_EVENT   13

#ifdef __cplusplus
}
#endif

#endif

